<?php
/**
 * 会员开到的宝藏
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\ActivityCouponMod;
use Model\ActivityMemberTreasureMod;
use Model\ActivityTreasureMod;
use Model\StoreCouponMod;

class MemberTreasure extends Action
{
    private $ActivityMemberTreasure = null;
    private $Store = null;
    private $StoreCoupon = null;
    private $ActivityCoupon = null;
    private $CouponSnapshot = null;  //卡券快照

	public function __construct()
	{
		parent::__construct();
        $this->ActivityMemberTreasure = new ActivityMemberTreasureMod();
	}

    public function getList()
    {
        $_where = array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'],'type'=>$this->_gpc['type'],'source'=>$this->_gpc['source']);
        $_list = $this->ActivityMemberTreasure->findAll($_where,array('order'=>array('id','desc')),array('treasure_id','number','exchange'));
        if(count($_list) > 0){
            $Treasure = null;
            if($this->_gpc['type'] == 1 || $this->_gpc['type'] == 2){
                $Treasure = new ActivityTreasureMod();
            }else if($this->_gpc['type'] == 3){
                $Treasure = new ActivityCouponMod();
            }
            foreach($_list as $_k=>$_v){
                $_treasure = $this->bzDetail($Treasure,$_v['treasure_id']);
                $_v['number'] = abs($_v['number']);
                $_v['exchange'] = abs($_v['exchange']);
                $_list[$_k] = array_merge($_v,$_treasure);
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 宝藏详情（宝藏和实物奖励）
     * @param $Treasure 宝藏对象
     * @param $_treasure_id 宝藏id
     * @return 宝藏详情
     */
    private function bzDetail($Treasure,$_treasure_id)
    {
        $_treasure = $Treasure->findOne(array('id'=>$_treasure_id),array('name','icon','money'));
        $_treasure['icon'] = tomedia($_treasure['icon']);
        $_treasure['money'] = abs($_treasure['money']);
        return $_treasure;
    }

    /**
     * 卡券详情
     * @param $Treasure 宝藏对象
     * @param $_treasure_id 宝藏id
     * @param $_expires  卡券的过期时间时间戳
     * @return 卡券详情
     */
    private function couponDetail($Treasure,$_treasure_id,$_expires)
    {
        $_put_coupon = $Treasure->findOne(array('id'=>$_treasure_id),array('coupon_id','use_limit_type'));
        $this->StoreCoupon = new StoreCouponMod();
        $_coupon = $this->StoreCoupon->findOne(array('id'=>$_put_coupon['coupon_id']),array('name','type','cost'));
        if($_put_coupon['use_limit_type'] == 1){
            $_coupon['time'] = '永久有效';
        }else{
            $_coupon['time'] = date('Y-m-d',$_expires);
        }
        return $_coupon;
    }


    /**
     * 获取一个会员获奖记录的指定数据
     */
    public function easyFind()
    {
        $_return = array();
        if(isset($this->_gpc['return'])){
            $_return = $this->_gpc['return'];
        }
        $_one = $this->ActivityMemberTreasure->findOne(array('id'=>$this->_gpc['member_treasure_id']),$_return);
        if($_one){
            $this->_result = array('status'=>'success','data'=>$_one);
        }
        $this->result($this->_result);
    }

}